﻿#include "OperFESensor.h"
#include "GUIDialog/GUISubSystemDialog/GUIFESensorDialog.h"
#include "PickDataProvider/PickedDataProvider.h"
#include "PickDataProvider/GUIPickInfo.h"
#include "PickDataProvider/PickedData.h"
#include "OperatorsInterface/GraphEventOperator.h"
#include "GUIFrame/MainWindow.h"
#include "GUIDialog/GUISubSystemDialog/GUIFESensorDialog.h"

#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsSensors.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsFEModel.h"

#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKAppFramework/FITKMessage.h"
#include "updateBrowserTable.h"

#include <QAction>

namespace ModelOper
{

    bool OperFESensor::execGUI()
    {

        if (!_emitter)return false;
        //获取动作名称
        QString actionName = _emitter->objectName();
        bool actionOK = false;
        //拾取动作，之后创建数据
        if (actionName.contains("Create"))
            actionOK = this->pickDataOper();
        //修改问题
        else if (actionName.contains("Edit"))
            actionOK = this->editSensorOper();
        return false;
    }

    bool OperFESensor::execProfession()
    {
        //获取数据id
        int objID = 0;
        if (!this->argValue("objID", objID)) return false;
        QString operType;
        if (!this->argValue<QString>("OperModelType", operType)) return false;
        //获取渲染数据
        EventOper::GraphEventOperator* graphOper = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
        if (graphOper == nullptr)return false;
        //是否需要初始化渲染数据
        //Append by BaGuijun(传感器方向更改需要传true)
        //bool forceUpdate = false;
        //if (operType == "Create")
        //    forceUpdate = true;
        //graphOper->updateGraph(objID, forceUpdate);
        graphOper->updateGraph(objID, true);

        //刷新表格
        ModelOper::updateBrowserTable();

        return true;
    }

    bool OperFESensor::pickDataOper()
    {
        //获取拾取器，绑定拾取结束信号
        GraphData::PickedDataProvider* pickProvider = GraphData::PickedDataProvider::getInstance();
        QAction* action = dynamic_cast<QAction*>(_emitter);
        if (!pickProvider || !action) return false;
        //清除拾取对象所有的连接
        QObject::disconnect(pickProvider, SIGNAL(sig_pickedOver()), nullptr, nullptr);
        //获取选择
        if (!action->isChecked())
        {
            //清除拾取模式和高亮
            GUI::GUIPickInfo::ClearPickInfo();
            disconnect(pickProvider, &GraphData::PickedDataProvider::sig_pickedOver, this, &OperFESensor::pickDataFinished);
            return false;
        }
        connect(pickProvider, &GraphData::PickedDataProvider::sig_pickedOver, this, &OperFESensor::pickDataFinished);
        //设置拾取方式
        GUI::GUIPickInfoStru pickInfo = GUI::GUIPickInfo::GetPickInfo();
        pickInfo._pickObjType = GUI::GUIPickInfo::PickObjType::POBJFEStructure;
        pickInfo._pickMethod = GUI::GUIPickInfo::PickMethod::PMSingle;
        GUI::GUIPickInfo::SetPickInfo(pickInfo);
        return true;
    }

    bool OperFESensor::editSensorOper()
    {
        GUI::MainWindow * mainWindow = dynamic_cast<GUI::MainWindow *>(FITKAPP->getGlobalData()->getMainWindow());
        if (!mainWindow)return false;
        int dataID = 0;
        if (!this->argValue("DataID", dataID))return false;
        this->setArgs("OperModelType", "Edit");
        //获取离散数据
        Acoustics::FITKAcousticsDiscModel* discModel = FITKAPP->getGlobalData()->getMeshData<Acoustics::FITKAcousticsDiscModel>();
        if (!discModel)return false;
        //获取FE传感器数据
        Acoustics::FITKAcousticsFEModel* feModel = discModel->getFEModel();
        if (!feModel)return false;
        Acoustics::FITKAcousticsSensorsManager* sensorMgr = feModel->getSensorManager();
        if (!sensorMgr) return false;
        Acoustics::FITKAcousticsSensors* dataSensorData = sensorMgr->getDataByID(dataID);
        if (!dataSensorData)return false;
        //创建传感器窗口
        GUI::GUIFESensorDialog* dialog = new GUI::GUIFESensorDialog(dataSensorData, this, mainWindow);
        connect(dialog, &QDialog::accepted, this, &OperFESensor::execProfession);
        dialog->show();
        return true;
    }

    void OperFESensor::pickDataFinished()
    {
        QAction* action = dynamic_cast<QAction*>(_emitter);
        if (action)
            action->setChecked(false);
        //获取三维可视化对象管理
        GraphData::PickedDataProvider* pickProvider = GraphData::PickedDataProvider::getInstance();
        if (!pickProvider)return;
        //取消关联信号
        disconnect(pickProvider, &GraphData::PickedDataProvider::sig_pickedOver, this, &OperFESensor::pickDataFinished);
        //获取拾取数据对象
        QList<GraphData::PickedData*> pickList = pickProvider->getPickedList();
        //拾取FE数据ID
        int objID = -1;
        QList<GraphData::PickedFaceData> pickDataIndex{};
        for (GraphData::PickedData* pickData : pickList)
        {
            if (!pickData) continue;
            objID = pickData->getPickedDataObjId();
            pickDataIndex = pickData->getPickedSEAFaces();
        }
        if (objID == -1)
        {
            AppFrame::FITKMessageError("Sensor Create Err !!! pick data Err !");
            return;
        }
        if (pickDataIndex.isEmpty()) return;
        this->setArgs("pickDataObjID", objID);
        int elementID = pickDataIndex.at(0).ElementId;
        this->setArgs("ElementID", elementID);

        createSensorOper();
    }

    void OperFESensor::createSensorOper()
    {
        this->setArgs("OperModelType", "Create");
        //获取主窗口
        GUI::MainWindow * mainWindow = dynamic_cast<GUI::MainWindow *>(FITKAPP->getGlobalData()->getMainWindow());
        if (!mainWindow) return;
        //创建传感器窗口
        GUI::GUIFESensorDialog* dialog = new GUI::GUIFESensorDialog(this, mainWindow);
        connect(dialog, &QDialog::accepted, this, &OperFESensor::execProfession);
        dialog->show();
    }
}

